//
//  LifeCircleViewController.m
//  meiyinxiang
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 hbjApple. All rights reserved.
//  生活圈首页viewcontrollers

#import "LifeCircleViewController.h"
#import "VoteHomepageController.h"

#import <MJRefresh.h>
#import "UIImage+Category.h"
#import "LCHotListTableViewCell.h"
#import "LCSubListTableViewCell.h"
#import "TopHotestListVIewModel.h"


#define LCHotListTableViewCellReuseId  @"LCHotListTableViewCellReuseId"
#define LCSubListTableViewCellReuseId @"LCSubListTableViewCellReuseId"
@interface LifeCircleViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    TopHotestListVIewModel *hotListViewModel;
    BOOL isFirstTime;
}

@property (nonatomic,strong) UITableView *tableview;


@end

@implementation LifeCircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configTableViewCell];
    if (hotListViewModel == nil) {
        hotListViewModel = [[TopHotestListVIewModel alloc] init];
    }
    [self loadData];
    [self addRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -PrivateMethod
- (void)addRefresh{
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
}
- (void)loadData{
    isFirstTime = YES;
    [self.tableview.mj_header beginRefreshing];
    [hotListViewModel loadTopListWithResult:^(BOOL isSucess) {
        if (isSucess) {
            [self.tableview reloadData];
        }
        [self.tableview.mj_header endRefreshing];
    }];
}
- (void)configTableViewCell{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight-TABBARHEIGHT) style:UITableViewStylePlain];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
   
    [self.tableview registerNib:[UINib nibWithNibName:@"LCHotListTableViewCell" bundle:nil] forCellReuseIdentifier:LCHotListTableViewCellReuseId];
    [self.tableview registerNib:[UINib nibWithNibName:@"LCSubListTableViewCell" bundle:nil] forCellReuseIdentifier:LCSubListTableViewCellReuseId];
    
}

#pragma mark -UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 210;
    }else{
        return ScreenWidth > 375 ? 165 : 150;
    }
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        LCHotListTableViewCell *hotCell = [tableView dequeueReusableCellWithIdentifier:LCHotListTableViewCellReuseId forIndexPath:indexPath];
        if (hotCell == nil) {
            hotCell = [LCHotListTableViewCell loadNibHotlistCell];
        }
      //  if (hotListViewModel.modelArray.count > 0 && isFirstTime) {
            hotCell.topModelArray = hotListViewModel.modelArray;
            isFirstTime = NO;

      //  }

        return hotCell;
    }else{
        LCSubListTableViewCell *subHotCell = [tableView dequeueReusableCellWithIdentifier:LCSubListTableViewCellReuseId forIndexPath:indexPath];
        if (subHotCell == nil) {
            subHotCell = [LCSubListTableViewCell loadNibWithLCSubCell];
        }
        subHotCell.indexPath = indexPath;
        return subHotCell;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        VoteHomepageController *voteHompageVC = [VoteHomepageController loadNibWithVoteVC];
        
        [self.navigationController pushViewController:voteHompageVC animated:YES];
    }
}

@end
